package com.csu.Controller;

import com.csu.model.Account;
import com.csu.model.Cart;
import com.csu.model.CartItem;
import com.csu.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@SessionAttributes(value = {"loginAccount","cart"})
public class CartController {
    @Autowired
    private CartService cartService;

    //页面跳转
    @GetMapping("/cartForm")
    public String cartForm(Model model) {
        Account account = (Account) model.getAttribute("loginAccount");
        Cart cart = new Cart();
        assert account != null;         //断言account必不为空
        cart.setItemList(cartService.checkCartList(account.getUsername()));
        model.addAttribute("cart", cart);
        return "cart/cart";
    }

    //更新购物车
    @PostMapping("/updateCart")
    public String updateCart(Model model) {

        return "cart/cart";
    }

    //删除购物车
    @GetMapping("/removeItemFromCart")
    public String removeCart(@RequestParam String itemId , Model model) {
        Account account = (Account) model.getAttribute("loginAccount");
        assert account != null;         //断言account必不为空
        cartService.removeCart(account.getUsername(), itemId);
        return "cart/cart";
    }
}
